package com.mkjli.ScheduleManager.bo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.mkjli.ScheduleManager.dao.ScheduleDAO;
import com.mkjli.ScheduleManager.model.TaskModel;

public class ScheduleBO {

	@Autowired
	private ScheduleDAO scheduleDAO;

	public Map<String, String> getCalendarData(String year, String month) {
		Calendar first = new GregorianCalendar(new Locale("KO"));
		Calendar last = new GregorianCalendar(new Locale("KO"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		first.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
		first.add(Calendar.DATE, -1 * first.get(Calendar.DAY_OF_WEEK));
		String start = sdf.format(first.getTime());

		int lastDate = first.getActualMaximum(Calendar.DATE);
		last.set(Integer.parseInt(year), Integer.parseInt(month) - 1, lastDate);
		int countWeek = first.get(Calendar.WEEK_OF_MONTH);
		first.add(Calendar.DATE, countWeek * 7);
		String end = sdf.format(first.getTime());
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("year", year);
		result.put("month", month);
		result.put("start", start);
		result.put("end", end);
		result.put("weekCount", Integer.toString(countWeek));
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getScheduleData(Map<String, String> calendarData) throws ParseException {
		String start = (String)calendarData.get("start");
		String end = (String)calendarData.get("end");
		String count = (String)calendarData.get("weekCount");
		List<TaskModel> tasks = selectTasks(start, end);

		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Map<String, Object>> pointer = new HashMap<String, Map<String, Object>>();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = sdf.parse(start);
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		for (int i = 0; i < Integer.parseInt(count) * 7; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			cal.add(Calendar.DATE, 1);
			map.put("year", cal.get(Calendar.YEAR));
			map.put("month", cal.get(Calendar.MONTH) + 1);
			map.put("date", cal.get(Calendar.DATE));
			List<TaskModel> taskList = new ArrayList<TaskModel>();
			String ymd = sdf.format(cal.getTime());
			map.put("ymd", ymd);
			map.put("taskList", taskList);
			result.add(map);
			pointer.put(ymd, map);
		}
		for (TaskModel task : tasks) {
			((List<TaskModel>)pointer.get(task.getYmd()).get("taskList")).add(task);
		}
		return result;
	}

	public void insertTask(TaskModel task) {
		scheduleDAO.insertTask(task);
	}

	public TaskModel selectLastTask() {
		return scheduleDAO.selectLastTask();
	}

	public List<TaskModel> selectTasks(String start, String end) {
		return scheduleDAO.selectTasks(start, end);
	}

	public void updateTask(TaskModel task) {
		scheduleDAO.updateTask(task);
	}
}
